package com.hanter.android.radwidget.recyclerview;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;

import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

public abstract class RecyclerAdapter<T> extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    protected List<T> dataSet;

    protected View.OnClickListener onItemClickListener;

    public RecyclerAdapter() {
        super();
        this.dataSet = new ArrayList<>();
    }

    protected View createItemView(ViewGroup parent, int viewType) {
        return LayoutInflater.from(parent.getContext()).inflate(getItemLayoutId(viewType), parent, false);
    }

    @LayoutRes
    public abstract int getItemLayoutId(int viewType);

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = createItemView(parent, viewType);
        return newViewHolder(itemView, viewType);
    }

    public abstract RecyclerView.ViewHolder newViewHolder(@NonNull View itemView, int viewType);

    public T getItem(int position) {
        return dataSet == null ? null : dataSet.get(position);
    }

    @Override
    public int getItemCount() {
        return dataSet == null ? 0 : dataSet.size();
    }

    public List<T> getDataSet() {
        return this.dataSet;
    }

    public void setDataSet(List<T> dataSet) {
        this.dataSet = dataSet;
    }

    public void addCollection(@Nullable Collection<? extends T> collection) {
        if (collection != null) {
            this.dataSet.addAll(collection);
        }
    }

    @SafeVarargs
    public final void addCollection(T... collection) {
        int var3 = collection.length;
        this.dataSet.addAll(Arrays.asList(collection).subList(0, var3));
    }

    public void add(T t) {
        this.dataSet.add(t);
    }

    public void add(T t, int position) {
        this.dataSet.add(position, t);
    }

    public void remove(int position) {
        this.dataSet.remove(position);
    }

    public void removeAll() {
        this.dataSet.clear();
    }

    public void clear() {
        this.dataSet.clear();
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public static int getViewItemPosition(RecyclerView recyclerView, View v) {
        RecyclerView.ViewHolder holder = recyclerView.findContainingViewHolder(v);
        int position = AdapterView.INVALID_POSITION;
        if (holder != null) {
            position = holder.getAdapterPosition();
        }
        return position;
    }

    public static abstract class OnItemClickListener implements View.OnClickListener {

        private RecyclerView recyclerView;

        public OnItemClickListener(RecyclerView recyclerView) {
            this.recyclerView = recyclerView;
        }

        @Override
        public void onClick(View v) {
            RecyclerView.ViewHolder holder = recyclerView.findContainingViewHolder(v);

            int position = AdapterView.INVALID_POSITION;

            if (holder != null) {
                position = holder.getAdapterPosition();
            }

            onItemClick(v, position);
        }


        public abstract void onItemClick(View v, int position);
    }

}
